package com.jar.springbootsecuritydemo.aop;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class AuthorizationExceptionHandler {
    @ExceptionHandler(value = UsernameNotFoundException.class)
    public Object usernameNotFoundExceptionHanler(UsernameNotFoundException e) {
        Map<String, Object> result = new HashMap<>(3);
        result.put("code", 200);
        result.put("msg", "没有找到用户名");
        result.put("data", e.getMessage());
        return result;

    }

    @ExceptionHandler(value = BadCredentialsException.class)
    public Object badCredentialsExceptionHanler(BadCredentialsException e) {
        Map<String, Object> result = new HashMap<>(3);
        result.put("code", 200);
        result.put("msg", "密码错误");
        result.put("data", e.getMessage());
        return result;

    }

    @ExceptionHandler(value = LockedException.class)
    public Object lockedExceptionHanler(LockedException e) {
        Map<String, Object> result = new HashMap<>(3);
        result.put("code", 200);
        result.put("msg", "用户被锁定");
        result.put("data", e.getMessage());
        return result;
    }

}
